package com.rtf.framework.web.mvc.rest.dict;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.rtf.framework.common.util.UtilCollection;
import com.rtf.framework.common.util.UtilMap;
import com.rtf.framework.common.util.UtilString;
import com.rtf.framework.common.util.support.MapConverter;
import com.rtf.framework.dao.support.service.jpa.AppJPABaseDataServiceImpl;
import lombok.Getter;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 字典服务
 */
public class SysDictService extends AppJPABaseDataServiceImpl<SysDict, SysDictDao> {
    /**
     * 查询条件表达式
     */
    @Getter
    private Map<String, String> queryExpressions = new LinkedHashMap<String, String>() {
        {
            put("code", "code:EQ");
            put("status", "status:EQ");
            put("parentPath", "levelPath:RLIKE");
            put("parentId", "parentId:EQ");
        }
    };

    /**
     * 根据父级字典编码，查询下级字典项的kv
     *
     * @param parentCode
     * @return
     */
    public List<SysDict> queryChildDict(String parentCode, boolean allChildren) {
        if (UtilString.isBlank(parentCode)) {
            return Lists.newArrayList();
        }
        //查询父级字典编码
        Map<String, Object> searchParams = Maps.newHashMap();
        searchParams.put("code", parentCode);
        searchParams.put("status", 1);
        List<SysDict> parentDictList = this.queryAll(searchParams, null);

        SysDict parentDict = UtilCollection.isNotEmpty(parentDictList) ? parentDictList.get(0) : null;
        if (parentDict == null) {
            return Lists.newArrayListWithCapacity(1);
        }
        // 查询子节点编码
        Map<String, Object> childSearchParams = Maps.newHashMap();
        if (allChildren) {
            childSearchParams.put("parentPath", parentDict.getId());
        } else {
            childSearchParams.put("parentId", parentDict.getId());
        }
        childSearchParams.put("status", 1);
        List<SysDict> childSysDict = this.queryAll(childSearchParams, new String[]{"num_asc"});

        return childSysDict;
    }

    /**
     * 根据父级字典编码，查询下级字典项的kv
     *
     * @param parentCode
     * @return
     */
    public Map<String, String> queryChildKv(String parentCode) {
        return queryChildKv(parentCode, false);
    }

    /**
     * 根据父级字典编码，查询下级字典项的kv
     *
     * @param parentCode
     * @return
     */
    public Map<String, String> queryChildKv(String parentCode, boolean allChildren) {
        List<SysDict> childrenDicts = queryChildDict(parentCode, allChildren);

        // 将字典项转换成kv结构，并返回
        return UtilMap.toMap(childrenDicts, new MapConverter<SysDict, String, String>() {
            @Override
            public String getKey(SysDict item) {
                return item.getCode();
            }

            @Override
            public String getValue(SysDict item) {
                return item.getName();
            }
        });
    }

    /**
     * 根据code查询字典
     *
     * @param code
     * @return
     */
    public SysDict queryByCode(String code) {
        if (UtilString.isBlank(code)) {
            return null;
        }

        //查询编码对应的字典项
        Map<String, Object> searchParams = Maps.newHashMap();
        searchParams.put("code", code.trim().toLowerCase());
        searchParams.put("status", "1");
        List<SysDict> list = this.queryAll(searchParams);

        //校验字典编码对象是否存在
        return UtilCollection.isNotEmpty(list) ? list.get(0) : null;
    }

    /**
     * 查询所有的子级分类
     *
     * @param parentIds
     * @return
     */
    public List<SysDict> querySubList(Set<Long> parentIds) {
        if (UtilCollection.isEmpty(parentIds)) {
            return Lists.newArrayList();
        }
        return this.entityDao.queryClassifyByLevelPath(parentIds);
    }
}
